package cn.edu.suda.catalina.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/12/21.
 */
public class VisitedUtils<K, V> {

    private Map<K, List<V>> visited = new HashMap<>();

    private void putK(K k) {
        List<V> vList = new ArrayList<>();
        visited.put(k, vList);
    }

    private void putV(K k, V v) {
        visited.get(k).add(v);
    }

    private void rmV(K k, V v) {
        visited.get(k).remove(v);
    }

    private boolean isContainsV(K k, V v) {
        return visited.get(k).contains(v);
    }

    public boolean containsK(K k) {
        return visited.containsKey(k);
    }

    public boolean containsV(K k, V v) {
        return containsK(k) && isContainsV(k, v);
    }

    public void addK(K k) {
        if (!containsK(k)) {
            putK(k);
        }
    }

    public void addV(K k, V v) {
        if (!containsV(k, v)) {
            addK(k);
            putV(k, v);
        }
    }

    public void removeV(K k, V v) {
        if (containsV(k, v)) {
            rmV(k, v);
        }
    }
}
